package view;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.Arrays;

import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import darksideoftheloom.Attendee;
import darksideoftheloom.Event;
import darksideoftheloom.Judge;
import darksideoftheloom.Organizer;

/**
 * This Panel displays the input fields to allow a user to login to the
 * application.
 * 
 * @author Shane Kwon
 * @version 2013-03-09
 * 
 */
@SuppressWarnings("serial")
public class BeweaveLoginPanel extends JPanel {

    /**
     * The width of the panel.
     */
    private static final int WIDTH = 1024;

    /**
     * The height of the panel.
     */
    private static final int HEIGHT = 768;

    /**
     * Manually specifying size of textfields.
     */
    private static final int COLS = 20;

    /**
     * The password field.
     */
    private final JPasswordField my_password_field;

    /**
     * The username field.
     */
    private final JTextField my_username_field;

    /**
     * The index number of the BeweaveAttendeePanel in the CardLayout panel.
     * Used to set the user for login.
     */
    private static final int BAPINDEX = 3;

    /**
     * The login panel presents fields for user to enter their username and
     * password, or a button that will take them to a new screen to register.
     * 
     * @param the_weavent
     *            The weaving Event
     * @param the_panel
     *            the panel container, used for switching between panels
     */
    public BeweaveLoginPanel(final Event the_weavent, final JPanel the_panel) {
        super();
        setLayout(null);
        
        // creating Judge and Organizer for testing/demoing
        final Judge judge = new Judge("Judy", "555-555-5555", "12345 Fake St., Seattle, WA 98101",
                "judgejudy@example.com", "judy", "password", "Category 1");
        final Organizer organizer = new Organizer("root", "boot");

        final JButton login_button = new JButton(new AbstractAction("Login") {
            public void actionPerformed(final ActionEvent the_event) {
                for (Attendee attendee : the_weavent.getAttendees()) {
                    if (my_username_field.getText().equals(
                            attendee.getUsername())
                            && Arrays.equals(my_password_field.getPassword(),
                                    attendee.getPassword().toCharArray())) {
                        ((BeweaveAttendeePanel) the_panel
                                .getComponent(BAPINDEX)).login(attendee);
                        CardLayout cl = (CardLayout) the_panel.getLayout();
                        cl.show(the_panel, "attendee");
                    }
                }
                if (my_username_field.getText().equals(
                        judge.getUsername())
                        && Arrays.equals(my_password_field.getPassword(),
                                judge.getPassword().toCharArray())) {
                    ((BeweaveJudgePanel) the_panel
                            .getComponent(4)).login(judge);
                    CardLayout cl = (CardLayout) the_panel.getLayout();
                    cl.show(the_panel, "judge");
                    
                }
                else if (my_username_field.getText().equals(
                        organizer.getUserID())
                        && Arrays.equals(my_password_field.getPassword(),
                                organizer.getPassword().toCharArray())) { 
                    CardLayout cl = (CardLayout) the_panel.getLayout();
                    cl.show(the_panel, "organizer");
                }
                my_username_field.setText("");
                my_password_field.setText("");
            }
        });
        final JButton cancel_button = new JButton(new AbstractAction("Cancel") {
            public void actionPerformed(final ActionEvent the_event) {
                my_password_field.setText("");
                my_username_field.setText("");
            }
        });
        final JButton register_button = new JButton(new AbstractAction(
                "Register...") {
            public void actionPerformed(final ActionEvent the_event) {
                CardLayout cl = (CardLayout) the_panel.getLayout();
                cl.show(the_panel, "register");
            }
        });
        my_password_field = new JPasswordField();
        my_username_field = new JTextField();
        my_password_field.setColumns(COLS);
        my_username_field.setColumns(COLS);

        JLabel username_label = new JLabel("Username: ");
        JLabel pw_label = new JLabel("Password: ");
        JLabel newuser_label = new JLabel("New user?");

        add(username_label);
        add(my_username_field);
        add(pw_label);
        add(my_password_field);
        add(login_button);
        add(cancel_button);
        add(newuser_label);
        add(register_button);

        // TODO get rid of hardcoded values
        Insets insets = getInsets();
        Dimension size = username_label.getPreferredSize();
        username_label.setBounds(350 + insets.left, 250 + insets.top,
                size.width, size.height);
        size = my_username_field.getPreferredSize();
        my_username_field.setBounds(500 + insets.left, 250 + insets.top,
                size.width, size.height);
        size = pw_label.getPreferredSize();
        pw_label.setBounds(350 + insets.left, 275 + insets.top, size.width,
                size.height);
        size = my_password_field.getPreferredSize();
        my_password_field.setBounds(500 + insets.left, 275 + insets.top,
                size.width, size.height);

        size = login_button.getPreferredSize();
        login_button.setBounds(560 + insets.left, 400 + insets.top, size.width,
                size.height);
        size = cancel_button.getPreferredSize();
        cancel_button.setBounds(642 + insets.left, 400 + insets.top,
                size.width, size.height);
        size = newuser_label.getPreferredSize();

        newuser_label.setBounds(350 + insets.left, 600 + insets.top,
                size.width, size.height);
        size = register_button.getPreferredSize();
        register_button.setBounds(500 + insets.left, 600 + insets.top,
                size.width, size.height);

        setPreferredSize(new Dimension(WIDTH, HEIGHT));
    }
}
